library(readxl)
mydata <- read_xlsx("./Urbanisation.xlsx")
mydata <- as.data.frame(mydata)
head(mydata)
## Country Urbanisation GDP Population
## 1 Afghanistan 24.803 1928 33753500
## 2 Albania 57.434 10032 2882482
## 3 Algeria 70.848 14004 39543148
## 4 Angola 63.446 8689 28127724
## 5 Argentina 91.503 19502 43257064
## 6 Armenia 63.085 10042 2878598
Description:
library(GGally) # Load tools for scatterplot matrices
ggpairs(
mydata[c( "GDP", "Urbanisation","Population")], # Variables to display
upper = list(
continuous = wrap("smooth", method = "lm", se = FALSE, alpha = 0.6) # Scatterplots with OLS line
),
lower = list(continuous = "cor"), # Pearson correlation coefficients
diag = list(continuous = "densityDiag") # Density plots on the diagonal
) +
theme(
axis.text.x = element_text(angle = 45, hjust = 1, size = 7),
axis.text.y = element_text(size = 7)
)
fit1 <- lm(GDP ~ Urbanisation + Population,
data = mydata)
mydata$StdResid <- rstandard(fit1)
mydata$StdFitted <- scale(fit1$fitted.values)
library(car)
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
ylab = "Standardised residuals",
xlab = "Standardised fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
mydata$logGDP <- log(mydata$GDP)
mydata$logPopulation <- log(mydata$Population)
head(mydata)
## Country Urbanisation GDP Population StdResid StdFitted
## 1 Afghanistan 24.803 1928 33753500 0.2635747 -1.5999631
## 2 Albania 57.434 10032 2882482 -0.4273066 -0.1291570
## 3 Algeria 70.848 14004 39543148 -0.6684254 0.4742626
## 4 Angola 63.446 8689 28127724 -0.7364115 0.1410741
## 5 Argentina 91.503 19502 43257064 -1.0862852 1.4046976
## 6 Armenia 63.085 10042 2878598 -0.6363626 0.1254255
## logGDP logPopulation
## 1 7.564238 17.33459
## 2 9.213535 14.87416
## 3 9.547098 17.49290
## 4 9.069813 17.15227
## 5 9.878272 17.58267
## 6 9.214532 14.87281
library(GGally) # Load tools for scatterplot matrices
ggpairs(
mydata[c("logGDP", "Urbanisation", "logPopulation")], # Variables to display
upper = list(
continuous = wrap("smooth", method = "lm", se = FALSE, alpha = 0.6) # Scatterplots with OLS line
),
lower = list(continuous = "cor"), # Pearson correlation coefficients
diag = list(continuous = "densityDiag") # Density plots on the diagonal
) +
theme(
axis.text.x = element_text(angle = 45, hjust = 1, size = 7),
axis.text.y = element_text(size = 7)
)
fit2 <- lm(logGDP ~ Urbanisation + logPopulation,
data = mydata)
vif(fit2)
## Urbanisation logPopulation
## 1.009893 1.009893
mydata$StdResid <- rstandard(fit2)
hist(mydata$StdResid,
xlab = "Standardised residuals",
ylab = "Frequency",
main = "Histogram of standardised residuals")
mydata$StdFitted <- scale(fit2$fitted.values)
library(car)
scatterplot(y = mydata$StdResid, x = mydata$StdFitted,
ylab = "Standardised residuals",
xlab = "Standardised fitted values",
boxplots = FALSE,
regLine = FALSE,
smooth = FALSE)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mydata %>%
select(logGDP, Urbanisation, logPopulation) %>%
summary()
## logGDP Urbanisation logPopulation
## Min. : 6.356 Min. : 12.08 Min. :11.16
## 1st Qu.: 8.245 1st Qu.: 42.78 1st Qu.:15.19
## Median : 9.380 Median : 62.54 Median :16.17
## Mean : 9.223 Mean : 60.32 Mean :16.18
## 3rd Qu.:10.110 3rd Qu.: 77.42 3rd Qu.:17.25
## Max. :11.958 Max. :100.00 Max. :21.06
library(dplyr)
# Calculate Cook's distance
mydata$CooksD <- cooks.distance(fit2)
# Define Cook's distance threshold
cd_threshold <- 4/nrow(mydata)
# Display apartments with Cook's distance above the threshold
influential_units <- mydata %>%
filter(CooksD > cd_threshold) %>%
arrange(desc(CooksD)) %>%
select(Country, logGDP, Urbanisation, logPopulation, StdResid, CooksD)
head(influential_units, 6) %>%
mutate(StdResid = round(StdResid, 3),
CooksD = round(CooksD, 3))
## Country logGDP Urbanisation logPopulation StdResid
## 1 Saint Lucia 9.228671 18.515 12.07618 2.197
## 2 Sao Tome and Principe 8.173293 70.174 12.21177 -1.967
## 3 Sri Lanka 9.283870 18.256 16.87594 2.325
## 4 Barbados 9.379999 31.249 12.53572 1.695
## 5 Djibouti 7.986505 77.417 13.82176 -2.540
## 6 Seychelles 10.153857 55.400 11.50550 1.395
## CooksD
## 1 0.122
## 2 0.054
## 3 0.052
## 4 0.050
## 5 0.045
## 6 0.038
library(dplyr)
mydataC <- mydata %>%
filter(Country != "Saint Lucia")
fit3 <- lm(logGDP ~ Urbanisation + logPopulation,
data = mydataC)
summary(fit3)
##
## Call:
## lm(formula = logGDP ~ Urbanisation + logPopulation, data = mydataC)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.00952 -0.54510 0.05341 0.48154 1.84184
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.629571 0.649026 10.215 <2e-16 ***
## Urbanisation 0.042116 0.002783 15.134 <2e-16 ***
## logPopulation 0.002583 0.037166 0.069 0.945
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7754 on 161 degrees of freedom
## Multiple R-squared: 0.5911, Adjusted R-squared: 0.586
## F-statistic: 116.4 on 2 and 161 DF, p-value: < 2.2e-16
library(readxl)
Continent <- read_xlsx("Continent.xlsx")
head(Continent, 3)
## # A tibble: 3 × 2
## Country Continent
## <chr> <chr>
## 1 Afghanistan Asia
## 2 Albania Europe
## 3 Algeria Africa
library(dplyr)
mydataC <- left_join(mydataC, Continent, by = "Country")
head(mydataC, 3)
## Country Urbanisation GDP Population StdResid StdFitted
## 1 Afghanistan 24.803 1928 33753500 -0.2358813 -1.6145691
## 2 Albania 57.434 10032 2882482 0.1155446 -0.1088290
## 3 Algeria 70.848 14004 39543148 -0.1133021 0.4515981
## logGDP logPopulation CooksD Continent
## 1 7.564238 17.33459 0.00044517321 Asia
## 2 9.213535 14.87416 0.00004497929 Europe
## 3 9.547098 17.49290 0.00005072054 Africa
mydataC$Continent <- as.factor(mydataC$Continent)
summary(mydataC$Continent)
## Africa Asia Europe North America Oceania
## 50 45 40 17 2
## South America
## 10
fit4 <- lm(logGDP ~ Urbanisation + logPopulation + Continent,
data = mydataC)
anova(fit3, fit4)
## Analysis of Variance Table
##
## Model 1: logGDP ~ Urbanisation + logPopulation
## Model 2: logGDP ~ Urbanisation + logPopulation + Continent
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 161 96.808
## 2 156 66.313 5 30.495 14.348 1.491e-11 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(fit4)
##
## Call:
## lm(formula = logGDP ~ Urbanisation + logPopulation + Continent,
## data = mydataC)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.98612 -0.34852 -0.01648 0.32603 1.82593
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.847138 0.571240 11.986 < 2e-16 ***
## Urbanisation 0.033472 0.002763 12.115 < 2e-16 ***
## logPopulation -0.016311 0.033064 -0.493 0.62247
## ContinentAsia 0.840463 0.145181 5.789 3.77e-08 ***
## ContinentEurope 1.193030 0.155088 7.693 1.53e-12 ***
## ContinentNorth America 0.578103 0.191786 3.014 0.00301 **
## ContinentOceania 1.142988 0.483679 2.363 0.01935 *
## ContinentSouth America 0.274345 0.249032 1.102 0.27231
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.652 on 156 degrees of freedom
## Multiple R-squared: 0.7199, Adjusted R-squared: 0.7074
## F-statistic: 57.28 on 7 and 156 DF, p-value: < 2.2e-16